package com.cuit.bysj.action;

import java.util.List;

import com.cuit.bysj.entities.Item;
import com.cuit.bysj.entities.User;
import com.cuit.bysj.service.itemService;
import com.cuit.bysj.utils.JsonHelper;
import com.cuit.bysj.utils.Result;
import com.cuit.bysj.utils.SelectUtils;
import com.opensymphony.xwork2.ActionSupport;

public class ScoreManageAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String result;
	private itemService itemservice;
	private String content;
	private String state;
	private String id;
	private String percentArr;

	public String getPercentArr() {
		return percentArr;
	}

	public void setPercentArr(String percentArr) {
		this.percentArr = percentArr;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public itemService getItemservice() {
		return itemservice;
	}

	public void setItemservice(itemService itemservice) {
		this.itemservice = itemservice;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String openpage() {
		Result<User> Userresult = new Result<User>();

		Userresult.setSuccess(true);
		Userresult.setUrl("ScoreProj");

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";

	}

	@SuppressWarnings("unchecked")
	public String loadItemlist() {
		Result<Item> Userresult = new Result<Item>();

		Userresult.setItems(itemservice.itemlist());
		// 查询评教开启状态并放入返回的json中
		Userresult.setSuccess(itemservice.isOpen());

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	public String openaddItem() {
		Result<Item> Userresult = new Result<Item>();

		Userresult.setUrl("addItempage");

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";

	}

	public String addItem() {
		Item item = new Item(content);

		itemservice.addItem(item);

		Result<Item> Userresult = new Result<Item>();

		Userresult.setMsg("添加成功！");
		Userresult.setUrl("addItempage");

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	public String updateItem() {
		Item item = itemservice.findByid(Integer.parseInt(id));

		item.setContent(content);

		itemservice.update(item);

		Result<Item> Userresult = new Result<Item>();

		Userresult.setMsg("修改成功！");
		Userresult.setUrl("addItempage");

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	public String delItem() {
		Result<Item> Userresult = new Result<Item>();

		itemservice.delete(Integer.parseInt(id));
		Userresult.setSuccess(true);

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	public String toggle() {
		Result<Item> Userresult = new Result<Item>();

		if (state.equals("close")) {
			itemservice.updateState(false);
			Userresult.setMsg("关闭评教成功!");
		}
		if (state.equals("open")) {
			itemservice.updateState(true);
			Userresult.setMsg("开启评教成功!");
		}

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	@SuppressWarnings("unchecked")
	public String saveItempercent() {
		Result<Item> Userresult = new Result<Item>();

		List<String> list = SelectUtils.savePercent(percentArr);
		List<Item> list1 = itemservice.itemlist();

		for (int i = 0; i < list.size(); i++) {
			Item item = list1.get(i);
			item.setPercent(Integer.parseInt(list.get(i)));
			itemservice.update(item);
		}

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}
}
